package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.gzdh.dishrecognition.R;
import com.dhkj.web.bean.SysUserModel;

import java.util.ArrayList;
import java.util.List;

public class SelectPosAdapter extends BaseAdapter {

    List<SysUserModel> list;

    Context context;

    HolderView holderView = null;

    private static SparseBooleanArray isSelected; /**用SparseBooleanArray来代替map**/
    /**
     * 全选回调接口
     */
    CheckedAllListener mListener;

    public void setCheckedAllListener(CheckedAllListener listener) {
        mListener = listener;
    }


    public SelectPosAdapter(List<SysUserModel> list, Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.list = list;
        isSelected = new SparseBooleanArray();
        initData();
    }

    /**
     * 初始化数据
     */
    public void initData() {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCheck(false);
            getIsSelected().put(i, false);
        }
        notifyDataSetChanged();
    }

    public void checkAll() {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCheck(true);
            getIsSelected().put(i, true);
        }
        notifyDataSetChanged();
    }

    public static SparseBooleanArray getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(SparseBooleanArray isSelected) {
        SelectPosAdapter.isSelected = isSelected;
    }

    public List<String> getPosNum() {
        List<String> posList = new ArrayList<>();
        for (SysUserModel sysUserModel : list) {
            if(sysUserModel.getCheck()) {
                posList.add(sysUserModel.getBh());
            }
        }
        return posList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public SysUserModel getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = convertView;
        if (view == null) {
            holderView = new HolderView();
            //得到资源文件
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_pos_num, parent, false);
            holderView.posRow = view.findViewById(R.id.pos_row);
            holderView.checkBtn = (CheckBox) view.findViewById(R.id.check_btn);
            holderView.posName = (TextView) view.findViewById(R.id.pos_name);
            view.setTag(holderView);
        } else {
            holderView = (HolderView) view.getTag();
        }
        final SysUserModel item = getItem(position);
        if (item != null) {
            String bh = item.getBh() != null ? item.getBh().trim() : "";
            String name = item.getName() != null ? item.getName().trim() : "";
            holderView.posName.setText(String.format("%s-%s", bh, name));
            holderView.checkBtn.setChecked(isSelected.get(position));
        }

        /**
         * 增加checkbox的改变事件，每个item的点击事件
         */
        holderView.posRow.setOnClickListener(view1 -> {
            if(item != null) {
                isSelected.put(position, !item.getCheck());
                item.setCheck(!item.getCheck());
                notifyDataSetChanged();
            }
        });
        holderView.checkBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (buttonView.isPressed()) {
                isSelected.put(position, isChecked);
                //监听回调，是否改变全选按钮的状态
                //mListener.CheckAll(isSelected);
            }
            if(item != null) {
                item.setCheck(isChecked);
            }
        });
        return view;
    }


    static class HolderView {
        private CheckBox checkBtn;
        private TextView posName;
        private View posRow;
    }

    /**
     * 当所有CheckBox全选时回调
     * @author Administrator
     *
     */
    public interface CheckedAllListener {
        void CheckAll(SparseBooleanArray isCheckAll);
    }
}
